package com.sk.bilibili.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.Glide;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.collect.CollectAdapter;
import com.sk.bilibili.bean.Collect;
import com.sk.bilibili.bean.Favorites;
import com.sk.bilibili.callBack.AppBarChangeCallBack;
import com.sk.bilibili.callBack.OnAppBarChangeListener;
import com.sk.bilibili.config.GlideApp;
import com.sk.bilibili.databinding.ActivityCollectBinding;
import com.sk.bilibili.model.activity.CollectModel;
import com.sk.bilibili.view.diy.bottomDialog.DeleteBottomDialog;
import com.sk.bilibili.view.diy.bottomDialog.UnCollectBottomDialog;

import java.util.List;

public class CollectActivity extends BaseActivity<ActivityCollectBinding, CollectModel> implements AppBarChangeCallBack {

    public static void start(Context context,Integer favoritesId){
        Intent intent = new Intent(context, CollectActivity.class);
        intent.putExtra("id",favoritesId);
        context.startActivity(intent);
    }

    private Integer favoritesId;
    private CollectAdapter collectAdapter;
    private UnCollectBottomDialog unCollectBottomDialog;

    @Override
    protected void afterSetView() {
        favoritesId= (Integer) getIntent().getSerializableExtra("id");
        model.loadData(favoritesId);
        initView();
        bindEvent();
    }

    private void initView() {
        unCollectBottomDialog=new UnCollectBottomDialog();
        bind.tvName.setVisibility(View.GONE);
        VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
        bind.rvShow.setLayoutManager(layoutManager);
        DelegateAdapter adapter = new DelegateAdapter(layoutManager);
        collectAdapter = new CollectAdapter(this, this);
        adapter.addAdapter(collectAdapter);
        bind.rvShow.setAdapter(adapter);
    }

    private void bindEvent() {
        bind.ivBack.setOnClickListener(v->finish());
        bind.tvPlay.setOnClickListener(v -> {
            //  TODO  播放页面
        });
        bind.ablShow.addOnOffsetChangedListener(new OnAppBarChangeListener(this));
    }

    public void loadFavoritesSuccess(Favorites favorites) {
        bind.tvName.setText(favorites.getTitle());
        bind.tvTitle.setText(favorites.getTitle());
        GlideApp.with(this).load(favorites.getCover()).img().into(bind.ivShow);
        bind.tvInfo.setText(String.format("创建者:%s",favorites.getExtra().getMember().getNickName()));
        bind.tvPlay.setText(String.format("播放全部(%d)",favorites.getCount()));
        bind.tvInfo.setOnClickListener(v->onMemberClick(favorites.getMemberId()));//必须结果返回才直到Id
    }

    public void onVideoSetClick(Integer id) {
        VideoSetActivity.start(this,id);
    }

    public void onMoreClick(Integer id) {
        unCollectBottomDialog.show(getSupportFragmentManager(),id);
    }

    public void loadCollectSuccess(List<Collect> records) {
        collectAdapter.setData(records);
    }

    @Override
    public void onExpanding() {
        bind.tvName.setVisibility(View.GONE);
    }

    @Override
    public void onClose() {
        bind.tvName.setVisibility(View.VISIBLE);
    }

    public void onMemberClick(Integer memberId) {
        MemberActivity.start(this,memberId);
    }

}